package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.SysUser;
import com.itheima.entity.Userprofile;
import com.itheima.result.PageResult;
import com.itheima.service.AccountManagementService;
import com.itheima.service.IUserprofileService;
import com.itheima.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 用户概况(Userprofile)表控制层
 *
 * @author guanhuaer
 * @since 2024-09-04 21:14:09
 */
@RestController
@RequestMapping("userprofile")
public class UserprofileController {
    /**
     * 服务对象
     */
    @Autowired
    private AccountManagementService managementService;
    @Autowired
    private IUserprofileService userprofileService;
    /**
     * 查询用户列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询用户列表", notes = "查询用户列表")
    public Result<PageResult> list(SysUser dto) {
        PageResult pageResult = managementService.list(dto);
        return Result.success(pageResult);
    }

    /**
     * 批量增加用户
     * @param users
     * @return
     */
    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量增加用户", notes = "批量增加用户")
    public Result batchAdd(@RequestBody List<SysUser> users) {
        managementService.batchAdd(users);
        return Result.success();
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    @ApiOperation(value = "批量删除用户", notes = "批量删除用户")
    public Result batchDelete(@RequestParam("ids") List<Integer> ids) {
        if (ids == null || ids.size() == 0){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        managementService.batchDelete(ids);
        return Result.success();
    }

    /**
     * 更新用户
     * @param sysUser
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新用户", notes = "更新用户")
    public Result batchUpdate(@RequestBody SysUser sysUser) {
        managementService.updateUser(sysUser);
        return Result.success();
    }

    /**
     * 增加浏览量何访问量
     * @param userprofile
     * @return
     */
     @PostMapping("/addVisitCount")
     @ApiOperation(value = "增加浏览量何访问量", notes = "增加浏览量何访问量")
     public Result addVisitCount(@RequestBody Userprofile userprofile) {
         userprofileService.addVisitCount(userprofile);
         return Result.success();
     }
    /**
     * 根据用户id查询访问量
     * @param projectIds
     * @return
     */
    @GetMapping("/getVisitViewsUsersCount")
    @ApiOperation(value = "根据用户id查询访问量", notes = "根据用户id查询访问量")
    public Result getVisitCount( Integer[] projectIds) {
        Map<String, Integer> userprofile = userprofileService.getVisitCount(projectIds);
        return Result.success(userprofile);
    }

    /**
     * 向用户发送请求，是否同意重置密码
     * @param user
     * @return
     */
    @PostMapping("/resetPasswordEmail")
    @ApiOperation(value = "向用户发送请求，是否同意重置密码", notes = "向用户发送请求，是否同意重置密码")
    public Result resetPasswordEmail(@RequestBody SysUser user) {
        userprofileService.resetPasswordEmail(user);
        return Result.success();
    }

    /**
     * 查询用户是否同意重置密码
     * @param userId
     * @return
     */
    @GetMapping("/checkResetPassword")
    @ApiOperation(value = "查询用户是否同意重置密码", notes = "查询用户是否同意重置密码")
    public Result checkResetPassword(Integer userId) {
        Integer isReset =  managementService.checkResetPassword(userId);
        return Result.success(isReset);
    }

}
